package com.yoomiito.app.widget

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import android.view.View

class ScrollLinearLayouManager(context: Context,orientation:Int = VERTICAL) : androidx.recyclerview.widget.LinearLayoutManager(context){
    val SELECTED = 1
    val RELEASE = 2
    private val mPagerSnapHelper = androidx.recyclerview.widget.PagerSnapHelper()
    var onPageScrollListener:(position:Int)->Unit={position ->  }
    override fun onAttachedToWindow(view: androidx.recyclerview.widget.RecyclerView?) {
        super.onAttachedToWindow(view)
        mPagerSnapHelper.attachToRecyclerView(view)
        view?.addOnChildAttachStateChangeListener(object : androidx.recyclerview.widget.RecyclerView.OnChildAttachStateChangeListener {

            override fun onChildViewDetachedFromWindow(p0: View) {

            }

            override fun onChildViewAttachedToWindow(p0: View) {

            }

        })
    }

    /**
     * 滑动状态的改变
     * 缓慢拖拽-> SCROLL_STATE_DRAGGING
     * 快速滚动-> SCROLL_STATE_SETTLING
     * 空闲状态-> SCROLL_STATE_IDLE
     *
     * @param state
     */
    override fun onScrollStateChanged(state: Int) {
        super.onScrollStateChanged(state)
        when (state) {
            androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE -> {
                val viewIdle = mPagerSnapHelper.findSnapView(this)
                if (viewIdle != null) {
                    val positionIdle = getPosition(viewIdle)
                    if (onPageScrollListener != null && childCount < 2) {
                        onPageScrollListener(positionIdle)
                    }
                }
            }
            androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_DRAGGING -> {
            }
            androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_SETTLING -> {
            }
            else -> {
            }
        }
    }
}